package com.sright.bcard.content.biz.service;

import com.sright.bcard.common.service.share.dto.content.*;
import com.sright.bcard.common.service.share.dto.pager.Pager;
import com.sright.bcard.common.service.share.dto.request.CuFaceCacheRequest;
import com.sright.bcard.common.service.share.dto.request.CardEditRequest;
import com.sright.bcard.common.service.share.dto.request.EnterpriseCardRequest;
import com.sright.bcard.common.service.share.dto.response.*;
import com.sright.bcard.common.service.share.dto.user.LoginDTO;
import com.sright.bcard.content.biz.manager.CardManager;
import com.sright.bcard.content.dal.dao.CardDAO;
import com.sright.bcard.content.dal.dao.UserDAO;
import com.sright.bcard.content.dal.entity.CardDO;
import com.sright.bcard.content.dal.entity.UserDO;
import com.sright.bcard.content.dal.result.CardListDTO;
import com.sright.bcard.content.share.request.CardParamRequest;
import com.sright.bcard.content.share.request.EnterpriseCardAddParam;
import com.sright.bcard.content.share.service.CardService;
import com.sright.bcard.common.service.share.dto.enterprise.IndustryDTO;
import com.sright.bcard.user.share.service.LoginService;
import com.sright.common.response.SrightResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class CardServiceImpl implements CardService {

    @Autowired
    private CardManager cardManager;

    @Autowired
    private LoginService loginService;

    @Autowired
    private CardDAO cardDAO;

    @Autowired
    private UserDAO userDAO;

    @Override
    public SrightResponse<Boolean> addPersonalCard(IndustryDTO industryDTO) {
        return SrightResponse.defaultSuccess(cardManager.addPersonalCard(industryDTO));
    }

    @Override
    public SrightResponse<CardFullDTO> cardPersonalDetail(Long personalUserId) {
        CardDTO cardDTO = new CardDTO();
        cardDTO.setUserId(personalUserId);
        return SrightResponse.defaultSuccess(cardManager.getCardFullDTO(cardDTO));
    }

    @Override
    public SrightResponse<CardFullDTO> getCardFullDTO(CardDTO cardDTO) {
        return SrightResponse.defaultSuccess(cardManager.getCardFullDTO(cardDTO));
    }

    @Override
    public SrightResponse<Boolean> cardZh(CardParamRequest cardParamRequest) {
        return SrightResponse.defaultSuccess(cardManager.cardZh(cardParamRequest));
    }

    @Override
    public SrightResponse<Boolean> cardEn(CardParamRequest cardParamRequest) {
        return SrightResponse.defaultSuccess(cardManager.cardEn(cardParamRequest));
    }

    @Override
    public SrightResponse<Long> addCardEnterprise(EnterpriseCardAddParam enterpriseCardAddParam) {
        return SrightResponse.defaultSuccess(cardManager.addCardEnterprise(enterpriseCardAddParam));
    }

    @Override
    public SrightResponse<Boolean> editCardEnterpriseZh(EnterpriseCardAddParam enterpriseCardAddParam) {
        return SrightResponse.defaultSuccess(cardManager.editCardEnterpriseZh(enterpriseCardAddParam));
    }

    @Override
    public SrightResponse<Boolean> editCardEnterpriseEn(EnterpriseCardAddParam enterpriseCardAddParam) {
        return SrightResponse.defaultSuccess(cardManager.editCardEnterpriseEn(enterpriseCardAddParam));
    }

    @Override
    public SrightResponse<Boolean> editFontColor(String color, Long userId) {
        return SrightResponse.defaultSuccess(cardManager.editPersonalCardFontColor(color,userId));
    }

    @Override
    public SrightResponse<DemoCardDTO> demoCard1() {
        return SrightResponse.defaultSuccess(cardManager.demoCard1());
    }

    @Override
    public SrightResponse<DemoCardDTO> demoCard2() {
        return SrightResponse.defaultSuccess(cardManager.demoCard2());
    }

    @Override
    public SrightResponse<DemoCardDTO> demoCard3() {
        return SrightResponse.defaultSuccess(cardManager.demoCard3());
    }

    @Override
    public SrightResponse<CardInfoDetailDTO> cardInfoDetail(Long userId, Long oldPersonalUserId, Long oldEnterpriseUserId) {
        return SrightResponse.defaultSuccess(cardManager.cardInfoDetail(userId,oldPersonalUserId, oldEnterpriseUserId));
    }

    @Override
    public SrightResponse<List<CardMaterialDetailDTO>> cardMaterialDetail(Long cardId) {
        return SrightResponse.defaultSuccess(cardManager.cardMaterialDetail(cardId));
    }

    @Override
    public SrightResponse<Long> addEnterpriseCard(EnterpriseCardRequest enterpriseCardRequest) {
        Long cardId = cardManager.addEnterpriseCard(enterpriseCardRequest);
        if(cardId == 0L) {
            return SrightResponse.defaultBusinessError("企业名片添加已到达上限");
        }
        return SrightResponse.defaultSuccess(cardId);
    }

    @Override
    public SrightResponse<Boolean> editEnterpriseCard(EnterpriseCardRequest enterpriseCardRequest) {
        return SrightResponse.defaultSuccess(cardManager.editEnterpriseCard(enterpriseCardRequest));
    }

    @Override
    public SrightResponse<Integer> deleteEnterpriseCard(EnterpriseCardRequest enterpriseCardRequest) {
        Integer integer = cardManager.deleteEnterpriseCard(enterpriseCardRequest);
        if(integer == -1) {
            return SrightResponse.defaultBusinessError("至少要有一条CuFace！");
        }
        return SrightResponse.defaultSuccess(integer);
    }

    @Override
    public SrightResponse<Boolean> updateEnterpriseCardName(EnterpriseCardRequest enterpriseCardRequest) {
        return SrightResponse.defaultSuccess(cardManager.updateEnterpriseCardName(enterpriseCardRequest));
    }

    @Override
    public SrightResponse<Pager<EnterpriseCardResponse>> list(EnterpriseCardRequest enterpriseCardRequest) {
        return SrightResponse.defaultSuccess(cardManager.list(enterpriseCardRequest));
    }

    @Override
    public SrightResponse<Boolean> deleteUserCompanyInfo(Long cardUserCompanyInfoId, Long userId) {
        return SrightResponse.defaultSuccess(cardManager.deleteUserCompanyInfo(cardUserCompanyInfoId,userId));
    }

    @Override
    public SrightResponse<Boolean> editCard(CardEditRequest cardEditRequest, Long adminId) {
        return SrightResponse.defaultSuccess(cardManager.editCard(cardEditRequest,adminId));
    }

    @Override
    public SrightResponse<CuFaceCacheResponse> cardInfoMaterialDetail(CuFaceCacheRequest cuFaceCacheRequest) {
        CuFaceCacheResponse cuFaceCacheResponse = new CuFaceCacheResponse();
        // 如果是该路径 则解析query里面的参数
        if(StringUtils.isNotEmpty(cuFaceCacheRequest.getPath()) && cuFaceCacheRequest.getPath().contains("pages/card/main")) {
            String query = cuFaceCacheRequest.getQuery();
            List<String> params = Arrays.asList(query.split("&"));
            Map<String, String> map = new HashMap<>();
            params.forEach(param -> {
                if("userId".equals(param.split("=")[0])) {
                    map.put(param.split("=")[0], param.split("=")[1]);
                }
                if("u".equals(param.split("=")[0])) {
                    map.put(param.split("=")[0], param.split("=")[1]);
                }
            });
            // 企业
            if("1".equals(map.get("u"))) {
                SrightResponse<CardInfoDetailDTO> response = cardInfoDetail(Long.valueOf(map.get("userId")), 0L, Long.valueOf(map.get("userId")));
                CuFaceCacheEnterpriseResponse cuFaceCacheEnterpriseResponse = new CuFaceCacheEnterpriseResponse();
                cuFaceCacheEnterpriseResponse.setCardInfoDetailDTO(response.getData());
                UserDO userDO = userDAO.selectById(Long.valueOf(map.get("userId")));
                List<CardListDTO> cardListDTOS = cardDAO.selectIdListByEnterpriseId(userDO.getEnterpriseId());
                if(CollectionUtils.isNotEmpty(cardListDTOS)) {
                    SrightResponse<List<CardMaterialDetailDTO>> listSrightResponse = cardMaterialDetail(cardListDTOS.get(0).getId());
                    cuFaceCacheEnterpriseResponse.setCardMaterialDetailDTOS(listSrightResponse.getData());
                    cuFaceCacheEnterpriseResponse.setEnterpriseCardId(Long.valueOf(map.get("userId")));
                    cuFaceCacheEnterpriseResponse.setEnterpriseCardId(cardListDTOS.get(0).getId());
                }
                cuFaceCacheResponse.setCuFaceCacheEnterprise(cuFaceCacheEnterpriseResponse);
            }
            // 个人
            if("2".equals(map.get("u"))) {
                SrightResponse<CardInfoDetailDTO> response = cardInfoDetail(Long.valueOf(map.get("userId")), Long.valueOf(map.get("id")), 0L);
                CuFaceCachePersonalResponse cuFaceCachePersonal = new CuFaceCachePersonalResponse();
                cuFaceCachePersonal.setCardInfoDetailDTO(response.getData());
                CardDO cardDO = cardDAO.selectByUserId(Long.valueOf(map.get("userId")));
                SrightResponse<List<CardMaterialDetailDTO>> listSrightResponse = cardMaterialDetail(cardDO.getId());
                cuFaceCachePersonal.setCardMaterialDetailDTOS(listSrightResponse.getData());
                cuFaceCachePersonal.setPersonalUserId(Long.valueOf(map.get("userId")));
                cuFaceCachePersonal.setPersonalCardId(cardDO.getId());
                cuFaceCacheResponse.setCuFaceCachePersonal(cuFaceCachePersonal);
            }
        } else {
            SrightResponse<LoginDTO> login = loginService.login(cuFaceCacheRequest.getCode());
            if(login.isSuccess() && login.getData() != null) {
                LoginDTO loginDTO = login.getData();
                // 个人名片
                SrightResponse<CardInfoDetailDTO> personalResponse = cardInfoDetail(loginDTO.getPersonalUserId(),
                        0L, 0L);
                if(personalResponse.isSuccess() && personalResponse.getData() != null) {
                    CardInfoDetailDTO cardInfoDetailDTO = personalResponse.getData();
                    CardDO cardDO = cardDAO.selectByUserId(loginDTO.getPersonalUserId());
                    SrightResponse<List<CardMaterialDetailDTO>> listSrightResponse = cardMaterialDetail(cardDO.getId());
                    if(listSrightResponse.isSuccess() && listSrightResponse.getData() != null) {
                        CuFaceCachePersonalResponse cuFaceCachePersonalResponse = new CuFaceCachePersonalResponse();
                        cuFaceCachePersonalResponse.setCardInfoDetailDTO(cardInfoDetailDTO);
                        cuFaceCachePersonalResponse.setCardMaterialDetailDTOS(listSrightResponse.getData());
                        cuFaceCachePersonalResponse.setPersonalUserId(loginDTO.getPersonalUserId());
                        cuFaceCachePersonalResponse.setPersonalCardId(cardDO.getId());
                        cuFaceCacheResponse.setCuFaceCachePersonal(cuFaceCachePersonalResponse);
                    }
                }
                // 企业名片
                SrightResponse<CardInfoDetailDTO> enterpriseResponse = cardInfoDetail(0L,
                        0L, loginDTO.getEnterpriseUserId());
                if(enterpriseResponse.isSuccess() && enterpriseResponse.getData() != null) {
                    CardInfoDetailDTO cardInfoDetailDTO = enterpriseResponse.getData();
                    CardDO cardDO = cardDAO.selectByUserId(loginDTO.getEnterpriseUserId());
                    SrightResponse<List<CardMaterialDetailDTO>> listSrightResponse = cardMaterialDetail(cardDO.getId());
                    if(listSrightResponse.isSuccess() && listSrightResponse.getData() != null) {
                        CuFaceCacheEnterpriseResponse cuFaceCacheEnterpriseResponse = new CuFaceCacheEnterpriseResponse();
                        cuFaceCacheEnterpriseResponse.setCardInfoDetailDTO(cardInfoDetailDTO);
                        cuFaceCacheEnterpriseResponse.setCardMaterialDetailDTOS(listSrightResponse.getData());
                        cuFaceCacheEnterpriseResponse.setEnterpriseUserId(loginDTO.getEnterpriseUserId());
                        cuFaceCacheEnterpriseResponse.setEnterpriseCardId(cardDO.getId());
                        cuFaceCacheResponse.setCuFaceCacheEnterprise(cuFaceCacheEnterpriseResponse);
                    }
                }
            }
        }
        return SrightResponse.defaultSuccess(cuFaceCacheResponse);
    }

    @Override
    public SrightResponse<CardMaterialResponse> cardMaterialDetailNew(Long cardId) {
        return SrightResponse.defaultSuccess(cardManager.cardMaterialDetailNew(cardId));
    }

    @Override
    public SrightResponse<EnterpriseCardDetailResponse> enterpriseCardDetail(EnterpriseCardRequest enterpriseCardRequest) {
        return SrightResponse.defaultSuccess(cardManager.enterpriseCardDetail(enterpriseCardRequest));
    }

    @Override
    public SrightResponse cardConnectInfo(Long userId, Long oldPersonalUserId, Long oldEnterpriseUserId) {
        return SrightResponse.defaultSuccess(cardManager.cardConnectInfo(userId,oldPersonalUserId,oldEnterpriseUserId));
    }
}
